package cq.zhcqk.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.apache.http.Header;
import org.json.JSONObject;

import java.util.List;
import java.util.Map;

import cq.zhcqk.R;
import cq.zhcqk.util.AsyncHttpClientUtils;
import cq.zhcqk.util.Common;
import cq.zhcqk.util.GsonUtil;
import cq.zhcqk.util.ImageLoaderHelper;
import cq.zhcqk.util.StringUtil;
import cq.zhcqk.widget.DG_ListView;

/**
 * Created by zhangle on 2017/12/7.
 */
public class DzbJiaGouItemItemAdapter extends BaseAdapter{
    List<Map<String, Object>> list;
    Context context;
    private int selectItem = -1;
    private int sign = -1;
    private DzbJiaGouItemAdapter dzbJiaGouAdapter;
    public DzbJiaGouItemItemAdapter(Context context, List<Map<String, Object>> list){
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list ==null?0:list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final viewHolder holder;
        if(convertView == null){
            holder = new viewHolder();
            convertView = View.inflate(context, R.layout.item_list_list,null);
            holder.dz_img = (ImageView) convertView.findViewById(R.id.dz_img);
            holder.dz_title = (TextView) convertView.findViewById(R.id.dz_title);
            holder.dz_pname = (TextView) convertView.findViewById(R.id.dz_pname);
            holder.dz_lay = (LinearLayout) convertView.findViewById(R.id.dz_lay);
            convertView.setTag(holder);
        }else{
            holder = (viewHolder) convertView.getTag();
        }

        Map<String, Object> info = list.get(position);
        holder.dz_title.setText(info.get("name")+"");
        holder.dz_pname.setText(StringUtil.parseNull(info.get("templeader")+""));
        if(!"".equals(info.get("var01")+"")&&info.get("var01") != null){
            ImageLoader.getInstance().displayImage(Common.URL + info.get("var01")+"", holder.dz_img, ImageLoaderHelper.getOptionsDf());
        }
//        if(position == selectItem){//被选中的元素
//            if(sign == selectItem){//再次选中的时候会隐藏，并初始化标记位置
//                holder.dz_lay.setVisibility(View.GONE);
//                sign = -1;
//            }else{//选中的时候会展示，并标记此位置
//                holder.dz_lay.setVisibility(View.VISIBLE);
//                sign = selectItem;
//                getDetail(holder.listView, StringUtil.stringToInt(info.get("deptId")+"")+"");
//            }
//        }else {//未被选中的元素
//            holder.dz_lay.setVisibility(View.GONE);
//        }
        return convertView;
    }

    public void setSelectItem(int i){
        this.selectItem = i;
    }

    private class viewHolder{
        private TextView dz_title;
        private TextView dz_pname;
        private ImageView dz_img;
        private LinearLayout dz_lay;
    }
}
